﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework.Utils;
using CNVP.JShop.UI;

namespace CNVP.JShop.Web.Controls
{
    public partial class WriteReview : UserPage
    {
        public string productId;
        string OrderProductsID;
        protected void Page_Load(object sender, EventArgs e)
        {
            productId = Request.QueryString["ProductID"];
            if (string.IsNullOrEmpty(productId) || !Public.IsNumber(productId))
            {
                productId = "0";
            }
            OrderProductsID = Request.Params["OrderProductsID"];
            if (string.IsNullOrEmpty(OrderProductsID) || !Public.IsNumber(OrderProductsID))
            {
                OrderProductsID = "0";
            }
            if (!IsPostBack)
            {
                if (string.IsNullOrEmpty(CookieString))
                {
                    MessageUtils.ShowPre("您还未登录，请先登录。");
                }
                else
                {
                    string UserName = CookieString;
                    int BuyerID = CookieUserID;
                    BLL.CNVP_OrderProducts bll_OrderProducts = new BLL.CNVP_OrderProducts();
                    if (!string.IsNullOrEmpty(OrderProductsID) && OrderProductsID!="0")
                    {
                        var list = bll_OrderProducts.LoadAllByOrderProductsIDAndBuyer(Convert.ToInt32(OrderProductsID), BuyerID, "4,5,6,7");
                        if (list == null || list.Count <= 0)
                        {
                            MessageUtils.ShowPre("您无法评论。");
                            return;
                        }
                        else
                        {
                            OrderProductName.Visible = true;
                            for(int i=0;i<list.Count;i++)
                            {
                                LiterProductName.Text = list[i].ProductName;
                            }
                        }
                    }
                    else 
                    {
                        var list_Product = bll_OrderProducts.LoadAllByProductsIDAndBuyer(Convert.ToInt32(productId), BuyerID, "4,5,6,7");
                        if (list_Product == null || list_Product.Count <= 0)
                        {
                            MessageUtils.ShowPre("您无法评论。");
                            return;
                        }
                        else if (list_Product.Count >= 2)
                        {
                           Response.Write("<script>window.top.location.href='/Member/MyReview.aspx?ProductID=" + productId + "'</script>");
                        }
                    }
                }
            }


        }


        protected void submit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(CookieString))
            {
                MessageUtils.ShowPre("您还未登录，请先登录。");
            }
            else
            {
                string UserName = CookieString;
                int BuyerID = CookieUserID;

                BLL.CNVP_Review bll = new BLL.CNVP_Review();
                Model.CNVP_Review model = new Model.CNVP_Review();
                model.UserID = UserName;
                model.AddTime = DateTime.Now;
                model.IsDelete = 0;
                if (!String.IsNullOrEmpty(Config.UIConfig.ReviewIsApproved) && Config.UIConfig.ReviewIsApproved == "1")
                {
                    model.IsShow = 1;
                }
                else
                {
                    model.IsShow = 0;
                }
                model.IsTop = 0;
                model.ProductID = Convert.ToInt32(productId);
                model.ReviewContent = ReviewContent.InnerText;
                model.ReviewTitle = txtReviewTitle.Text;
                model.rateStatus = Convert.ToByte(Request.Form["rateStatus"]);
                int rows= bll.Add_CNVP_Review(model);
                if (rows > 0)
                {
                    BLL.CNVP_OrderProducts bll_OrderProducts = new BLL.CNVP_OrderProducts();
                    if (!string.IsNullOrEmpty(OrderProductsID) && OrderProductsID != "0")//会员中心中评论
                    {
                        var list = bll_OrderProducts.LoadAllByOrderProductsIDAndBuyer(Convert.ToInt32(OrderProductsID), BuyerID, "4,5,6,7");
                        if (list != null && list.Count > 0)
                        {
                            Model.CNVP_OrderProducts model_OrderProducts = new Model.CNVP_OrderProducts();
                            for (int i = 0; i < list.Count; i++)
                            {
                                model_OrderProducts.ID = list[i].ID;
                                model_OrderProducts.BuyerRate = 1;
                                model_OrderProducts.ReviewID = rows;
                                bll_OrderProducts.Update_CNVP_OrderProductsBuyerRate(model_OrderProducts);
                            }
                        }
                        else
                        {
                            MessageUtils.ShowPre("您无法评论。");
                            return;
                        }
                        Response.Write("<script>alert('评论成功，感谢您的评论！');window.top.location.href='/Member/MyReview.aspx'</script>");
                    }
                    else
                    {
                        var list_Product = bll_OrderProducts.LoadAllByProductsIDAndBuyer(Convert.ToInt32(productId), BuyerID, "4,5,6,7");
                        if (list_Product != null && list_Product.Count > 0)
                        {
                            Model.CNVP_OrderProducts model_OrderProducts = new Model.CNVP_OrderProducts();
                            for (int i = 0; i < list_Product.Count; i++)
                            {
                                model_OrderProducts.ID = list_Product[i].ID;
                                model_OrderProducts.BuyerRate = 1;
                                model_OrderProducts.ReviewID = rows;
                                bll_OrderProducts.Update_CNVP_OrderProductsBuyerRate(model_OrderProducts);
                            }
                        }
                        else
                        {
                            MessageUtils.ShowPre("您无法评论。");
                            return;
                        }
                        MessageUtils.ShowRedirect("评论成功，感谢您的评论！", "GetReviewList.aspx?id=" + productId);
                    }
                }
                
                
            }
            
        }
    }
}